﻿@Cofe ?= {}

$ = jQuery
Entry = Cofe.Entry
utils = Cofe.utils

# CONTROLLER - Represents a metadata in EntryMetaView.

class MetaItem extends Spine.Controller
  tag : "li"
  key : undefined
  value : undefined

  constructor : (param) ->
    super
    @key = utils.capitalise(param.key)
   
    #switch @key
    #  when "Tags" then @canEdit = true
    #  else @canEdit = false

    @isVisible = false
    @isEditing = false
    
    @type = typeof(@value)
    switch typeof(@value)
      when "object" then #do nothing
      when "array" then #donothing
      when "function" then #do nothing
      else switch @key
         when "Text" then #do nothing
         else @isVisible = true

    switch @key
      when "Tags" 
         @isVisible = true
         @value = $.csv.toArray(@value)
         #console.log @value
         @type = "array"


  render: () => 
    if @isVisible 
      @el.html(@template())      
    else @el.remove()

    dis = @
    $("a", @el).bind("click", () ->
        if dis.actionFunc isnt undefined
           dis.actionFunc(dis, @className)
    )

   
    return @el

  toggleEdit: () =>
    @isEditing = !@isEditing
    @render()

  remove: =>
    @el.remove()

  template: () =>
    #if @templateKey is undefined then throw "templateKey not specified"
    
    return @templateKey.render(new Object({
        key : @key,
        value : @value,
        type : @type,
        actions : @actions
        canEdit : @canEdit
        isEditing : @isEditing
    }))
 
    
Cofe.MetaItem = MetaItem